package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVoService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVo;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyEsService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVoService strategyStatisVoService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyEsService strategyEsService;

    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> commends = strategyCommendService.getTop5();
        model.addAttribute("commends",commends);

        //hotCds 热门排行
        List<StrategyStatisVo> hotCds = strategyStatisVoService.getHotStrategyTop10();
        model.addAttribute("hotCds",hotCds);

        //国外排行 abroadCds
        List<StrategyStatisVo> abroadCds = strategyStatisVoService.getAbroadStrategyTop10();
        //国内排行 unabroadCds
        List<StrategyStatisVo> unabroadCds = strategyStatisVoService.getInlandStrategyTop10();

        model.addAttribute("abroadCds",abroadCds);

        model.addAttribute("unabroadCds",unabroadCds);

        //攻略主题排行
        List<Map<String,Object>> themeCds = strategyEsService.queryThemeCommend();
        model.addAttribute("themeCds",themeCds);

        //chinas 国内攻略搜索条件列表
        List<StatisVO> chinas = strategyEsService.queryConditionChina();
        model.addAttribute("chinas",chinas);

        //abroads 国外攻略搜索条件列表
        List<StatisVO> abroads = strategyEsService.queryConditionAbroad();
        model.addAttribute("abroads",abroads);

        //themes  主题攻略搜索条件列表
        List<StatisVO> themes = strategyEsService.queryConditionTheme();
        model.addAttribute("themes",themes);

        return "strategy/index";
    }

    /**
     * 攻略明细
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public String detail(Model model,Long id){

        //攻略明细
        StrategyDetail detail = strategyDetailService.get(id);
        StrategyContent content = strategyDetailService.getContentByDetailId(detail.getId());
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        //阅读数
        strategyStatisVoService.increaseViewNum(id,1);

        //获取vo对象
        StrategyStatisVo strategyStatisVo = strategyStatisVoService.getStrategyStatisVo(id);

        model.addAttribute("vo",strategyStatisVo);

        //热门排行中阅读数和评论数的分数增加
        strategyStatisVoService.addHotStrategyScore(id,1);


        return "strategy/detail";
    }

    /**
     * 攻略列表
     * @param model
     * @param qo
     * @return
     */
    @GetMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts",toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);

        //tags
        model.addAttribute("tags",strategyTagService.list());

        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";
    }

    /**
     * 评论添加
     * @param strategyComment
     * @param userInfo
     * @return
     */
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo userInfo){

        strategyComment.setCity(userInfo.getCity());
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyComment.setLevel(userInfo.getLevel());
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setUsername(userInfo.getNickname());

        strategyCommentService.save(strategyComment);


        //评论添加
        strategyStatisVoService.increaseReplyNum(strategyComment.getDetailId(),1);

        //获取统计数据对象
        StrategyStatisVo strategyStatisVo = strategyStatisVoService.getStrategyStatisVo(strategyComment.getDetailId());

        //热门排行中阅读数和评论数的分数增加
        strategyStatisVoService.addHotStrategyScore(strategyComment.getDetailId(),1);

        return AjaxResult.SUCCESS.addData(strategyStatisVo.getReplynum());
    }

    /**
     * 评论列表
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo){

        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page",page);

        return "strategy/commentTpl";
    }

    /**
     * 点赞接口
     * @param toid  mongodb中strategy_comment中的_id
     * @param userInfo
     * @return
     */
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,@UserParam UserInfo userInfo){

        strategyCommentService.commentThumbUp(toid,userInfo.getId());



        return AjaxResult.SUCCESS;
    }

    /**
     * 攻略收藏 取消收藏接口
     * @param sid 攻略明细id
     * @param userInfo 当前登录用户信息
     * @return 返回操作是否成功,102状态码(未登录状态码)以及数据统计对象vo
     */
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){

        //判断用户是否登录
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false,"请先登录");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        //取消收藏 收藏
        boolean ret = strategyStatisVoService.favor(sid,userInfo.getId());
        //返回数据统计对象
        StrategyStatisVo strategyStatisVo = strategyStatisVoService.getStrategyStatisVo(sid);

        //国内外攻略排行对应点赞和收藏的分数增加
        strategyStatisVoService.addInlandAndAbroadStrategyScore(sid,ret?1:-1);

        return new AjaxResult(ret,"").addData(strategyStatisVo);
    }

    /**
     * 攻略点赞(顶)接口
     * @param sid 攻略明细id
     * @param userInfo 当前登录用户信息
     * @return 返回操作是否成功,102状态码(未登录状态码)以及数据统计对象vo
     */
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        //判断用户是否登录
        if(userInfo == null){
            AjaxResult ajaxResult = new AjaxResult(false,"请先登录");
            ajaxResult.setCode(102);
            return ajaxResult;
        }
        //点赞(顶)  今天点赞(顶)过
        boolean ret = strategyStatisVoService.strategyThumbup(sid,userInfo.getId());
        StrategyStatisVo strategyStatisVo = strategyStatisVoService.getStrategyStatisVo(sid);

        //国内外攻略排行对应点赞和收藏的分数增加
        strategyStatisVoService.addInlandAndAbroadStrategyScore(sid,ret?1:0);



        //返回数据统计对象
        return new AjaxResult(ret,"").addData(strategyStatisVo);
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){
        Page page = strategyEsService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }
}
